<?php

class license_driver
{
	var $valid;
	var $license = Array();
	var $items = Array();
	var $points;
	var $upgrades;
	var $bans;
	var $licenseBans;

	function license_driver($driver)
	{
		global $lang, $db, $properties;
		
		include_once('includes/addMessage.php');
			
		//get license details
		$q = 'SELECT pf_drivername, l_image, l_name, COALESCE(SUM(ar_licensepoints),0) as l_points, l_about
			  FROM ' . LICENSES_TABLE . ', ' . LCUSERS_TABLE . ', ' . DRIVERNAME_TABLE . ', ' . INCIDENTADMINREVIEW_TABLE . ', ' . INCIDENTS_TABLE . ' 
	    	  WHERE u_id = user_id AND l_id = u_license AND ar_incident = in_id AND in_adminstatus = 3 AND ar_admin = 0 AND 
			 	    ar_licenseexpiry > NOW() AND ar_driver = u_id AND u_id = ' . $driver;
					
		$res = $db->sql_query($q);
		if($this->license = $db->sql_fetchrow($res))
		{
				
					
			include_once('includes/database_query.php');
			
			
			//Current Points
			$mess = new addMessage($lang['LicenseIncidentInfo'], $lang['LicenseCurrentPoints']);
			$emptyMess = new addMessage($lang['IncidentsNone'], '');
		
			$query = 'SELECT in_id, e_title, c_smallTitle, c_id, e_id, d_id, in_event, d_title, t_shortTitle, in_raisedtime, in_race, in_lap, 
							 ar_licensepoints, as_ruling, date_format(ar_licenseexpiry, "%M %e, %Y") as licenseexpiry
				  FROM ' . JUDGES_TABLE . ', ' . DIVISIONS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ', ' . INCIDENTS_TABLE .  ', ' . EVENTS_TABLE .  ', ' .
						   INCIDENTADMINREVIEW_TABLE . ', ' . TRACKS_TABLE . ', ' . INCIDENTADMINRULINGS_TABLE . '
						WHERE (c_id = ia_champ OR ia_champ = 0) AND ar_incident = in_id AND t_id = e_track AND
						       d_champid = c_id and d_id = in_division AND in_adminstatus = 3 AND in_event = e_id AND 
						       ar_licensepoints > 0 AND as_id = ar_verdict AND ar_driver = ' . $driver . '
				  GROUP BY in_id
				  ORDER BY ar_date ASC';
				  
			$columns = Array($lang['IncidentEvent'], $lang['IncidentDetails']);
			$columnWidths = Array('40%', '60%');
			$columnValues = Array('<a href="events.php?id={in_event}&mode=results&race={d_id}-{in_race}">{c_smallTitle} ' . 
								 '{d_title}<br>{e_title} ({t_shortTitle}) ' . $lang['ResultsRace'] . ' {in_race}</a>', 
								 $lang['Incident'] .  ' #{in_id} - {as_ruling} {ar_licensepoints} ' . $lang['ResultsPtsUnit'] . 
								 '<br>' . $lang['LicensePointsExpires'] . '{licenseexpiry}');
		
			$this->points = new database_query($mess,  $query, $columns, $columnValues, false, $columnWidths, $emptyMess);
			
			
			//License Upgrades
			$mess = new addMessage($lang['LicenseUpgradeInfo'],$lang['LicenseUpgrade']);
			$emptyMess = new addMessage($lang['LicenseUpgradeNone'],'');
		
			$query = 'SELECT n.l_minRaces, n.l_maxPoints, n.l_name, u_races, u_licensePoints 
				  FROM ' . LICENSES_TABLE . ' c, ' . LICENSES_TABLE . ' n, ' . LCUSERS_TABLE . ', ' . DRIVERNAME_TABLE . '
				  WHERE user_id = u_id AND u_license = c.l_id AND n.l_order < c.l_order AND u_id = ' . $driver . '
				  GROUP BY n.l_id
				  ORDER BY n.l_order ASC';
				  
			$columns = Array($lang['LicenseNext'], $lang['LicenseMinRaces'], $lang['LicenseCurrentRaces'], $lang['LicenseMaxPoints'], $lang['LicenseCurrentPoints']);
			$columnValues = Array('{l_name}', '{l_minRaces}', '{u_races}', '{l_maxPoints}', '{u_licensePoints}');
		
			$this->upgrades = new database_query($mess,  $query, $columns, $columnValues, false, '', $emptyMess);
			
			
			//Bans
			$mess = new addMessage($lang['LicenseBansInfo'],$lang['Bans']);
			$emptyMess = new addMessage($lang['LicenseBansNone'],'');
		
			$query = 'SELECT  b_comment, date_format(b_startDate, "%b %e, %Y") as b_startDateF, 
							 date_format(b_endDate, "%b %e, %Y") as b_endDateF, COALESCE(l_name,"' . $lang['AllEvents'] . '") as l_name
				  FROM ' .  BANS_TABLE . ' LEFT JOIN ' . LICENSES_TABLE . ' ON b_license = l_id 
				  WHERE b_user = ' . $driver . '
				  ORDER BY b_startDate DESC';
				  
			$columns = Array($lang['BanStartDate'], $lang['BanEndDate'], $lang['Events'], '');
			$columnWidths = Array('16%', '16%', '18%', '50%');
			$columnValues = Array('{b_startDateF}', '{b_endDateF}', '{l_name}', '{b_comment}');
		
			$this->bans = new database_query($mess,  $query, $columns, $columnValues, false, $columnWidths, $emptyMess);
			
			
			//Bans
			$mess = new addMessage($lang['BansThresholdsInfo'],$lang['BansThresholds']);
		
			$query = 'SELECT  bt_points, bt_duration, bt_cleared, COALESCE(bl.l_name,"' . $lang['AllEvents'] . '") as l_name
				  FROM ' .  LCUSERS_TABLE . ', '  .  LICENSES_TABLE . ' ul, ' . BANTHRESHOLDS_TABLE . ' LEFT JOIN ' . LICENSES_TABLE . ' bl ON bt_license =  bl.l_id 
				  WHERE (bt_license = 0 OR bl.l_order >= ul.l_order) AND ul.l_id = u_license AND u_id = ' . $driver . '
				  ORDER BY bt_points DESC';
			$columns = Array($lang['Events'], $lang['LicensePoints'], $lang['BanTDuration'], $lang['BanTCleared']);
			$columnValues = Array('{l_name}', '{bt_points}', '{bt_duration}', '{bt_cleared}');
		
			$this->licenseBans = new database_query($mess,  $query, $columns, $columnValues);
			
			$this->valid = true;
			
		}
		else
		{
			$this->valid = false;
		}
	}
	
	function render()
	{
		include_once 'includes/addMessage.php';
		
		global $lang, $skin_dir;
			
		if($this->valid)
		{
			if($this->license['l_points'] == '')
			{
				$this->license['l_points'] = 0;
			}	
			
			$license_title = $this->license['pf_drivername'] . ' - ' . $this->license['l_points'] . ' ' . $lang['ResultsPtsUnit'];
			$license_image = $this->license['l_image'] == '' ? '' : '<img src="' . $this->license['l_image'] . '"/>';
			
			include $skin_dir . 'tpl/license_driver.tpl';
			
			$mess = new addMessage(formatOutput($this->license['l_about']),$lang['LicenseAbout']);
			$mess->render();
		
		
			$this->upgrades->render();
			
			foreach($this->items as $value)
			{
				$value->render();
			}
			
			$this->points->render();
			
			$this->bans->render();
			
			$this->licenseBans->render();
		
		}
		else
		{
			
			$mess = new addMessage($lang['LicenseNoneInfo'],$lang['BanTLicense']);
			$mess->render();
		}
		
	}
	
}

?>